package Dao.impl;/*
 *徐韬
 *2021/10/9
 *10:58
 */

import Dao.productDao;
import Util.DruidUtil;
import Util.Page;
import entity.Category;
import entity.Product;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProductDaoimpl implements productDao {
    QueryRunner queryRunner =new QueryRunner();
    @Override
    public List<Product> selectAll(Page page) {
        String sql = "select pId, pName, pDate, pVender, pPrice, pType, product.cId cId,cName from product join category c on product.cId = c.cId limit ?,?;";
        try {
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStartRow(), page.getPageSize());
            if(query==null){
                return null;
            }
            List<Product> productList = new ArrayList<>();
            for (Map<String, Object> map : query) {
                Product product =new Product();
                Category category = new Category();
                BeanUtils.populate(product,map);
                BeanUtils.populate(category,map);
                product.setCategory(category);
                productList.add(product);
            }
            return productList;
        } catch (Exception e) {
           throw new RuntimeException(e.getMessage()+"在分页查询时出现异常");
        }
    }

    @Override
    public Long selectCount() {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select count(*) from product;",new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"在查询总行数时出现异常");
        }
    }

    @Override
    public int insert(Product product, Integer cId) {
        Object[] params = {product.getpName(),product.getpDate(),product.getpVender(),product.getpPrice(),product.getpType(),cId};
        try {
            return queryRunner.update(DruidUtil.getConnection(),"insert into product values(0,?,?,?,?,?,?)",params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增时出现异常");
        }
    }

    @Override
    public Product select(Integer pId) {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select * from product where pId=?", new BeanHandler<>(Product.class),pId);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询单个信息时出现异常");
        }
    }

    @Override
    public int update(Product product, Integer cId) {
        Object[] params = {product.getpName(),product.getpDate(),product.getpVender(),product.getpPrice(),product.getpType(),cId,product.getpId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(),"update product set pName=?,pDate=?,pVender=?,pPrice=?,pType=?,cId=? where pId = ?",params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改时出现异常");
        }

    }

    @Override
    public int delete(Integer id) {
        try {
            return queryRunner.update(DruidUtil.getConnection(),"delete from product where pId=?",id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除异常");
        }
    }
}
